﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManagermentSystem
{
    public partial class ReaderCategoryForm : Form
    {
        public ReaderCategoryForm()
        {
            InitializeComponent();
        }

        string connStr = "Server=127.0.0.1;Database=csharpbookdb;uid=root;pwd=123456;";

        private void btnBackMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ReaderCategoryForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainMenuForm.getInstance().Show();
        }

        private void ReaderCategoryForm_Load(object sender, EventArgs e)
        {
            DataBind();
        }
        private void DataBind()
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connStr))
                {
                    conn.Open();
                    MySqlCommand comm = conn.CreateCommand();
                    comm.CommandText = "select rdType 类别号, rdTypeName 类别名称, canLendQty 可借数量, canLendDay 可借天数 from readertype";
                    MySqlDataAdapter sda = new MySqlDataAdapter(comm);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    dgvRdCategory.DataSource = ds.Tables[0];
                    tbRdType.DataBindings.Clear();
                    tbRdTypeName.DataBindings.Clear();
                    tbCanLendQty.DataBindings.Clear();
                    tbCanLendDay.DataBindings.Clear();

                    tbRdType.DataBindings.Add("Text", ds.Tables[0], "类别号");
                    tbRdTypeName.DataBindings.Add("Text", ds.Tables[0], "类别名称");
                    tbCanLendQty.DataBindings.Add("Text", ds.Tables[0], "可借数量");
                    tbCanLendDay.DataBindings.Add("Text", ds.Tables[0], "可借天数");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作数据库出错 " + ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connStr))
                {
                    conn.Open();
                    MySqlCommand comm = conn.CreateCommand();
                    comm.CommandText = "insert into readertype values(@rdType, @rdTypeName, @canLendQty, @canLendDay)";
                    comm.Parameters.AddWithValue("@rdType", tbRdType.Text);
                    comm.Parameters.AddWithValue("@rdTypeName", tbRdTypeName.Text);
                    comm.Parameters.AddWithValue("@canLendQty", tbCanLendQty.Text);
                    comm.Parameters.AddWithValue("@canLendDay", tbCanLendDay.Text);

                    try
                    {
                        comm.ExecuteNonQuery();
                        MessageBox.Show("添加成功");
                        DataBind();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("添加失败 " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作数据库出错 " + ex.Message);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connStr))
                {
                    conn.Open();
                    MySqlCommand comm = conn.CreateCommand();
                    comm.CommandText = "select rdType 类别号, rdTypeName 类别名称, canLendQty 可借数量, canLendDay 可借天数 from readertype where rdTypeName like CONCAT(@rdTypeName, '%')";
                    comm.Parameters.AddWithValue("@rdTypeName", tbRdTypeName.Text);
                    MySqlDataAdapter sda = new MySqlDataAdapter(comm);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    dgvRdCategory.DataSource = ds.Tables[0];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作数据库出错 " + ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除吗?", "确定", messButton);

            if (dr == DialogResult.OK)
            {
                try
                {
                    using (MySqlConnection conn = new MySqlConnection(connStr))
                    {
                        conn.Open();
                        MySqlCommand comm = conn.CreateCommand();
                        comm.CommandText = "delete from readertype where rdType = @rdType";
                        comm.Parameters.AddWithValue("@rdType", tbRdType.Text);
                        comm.ExecuteNonQuery();
                        MessageBox.Show("删除成功");
                        DataBind();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败 " + ex.Message);
                }
            }
        }

        private void btnAlter_Click(object sender, EventArgs e)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(connStr))
                {
                    conn.Open();
                    MySqlCommand comm = conn.CreateCommand();
                    comm.CommandText = "update readertype set rdType = @rdType, rdTypeName = @rdTypeName, canLendQty = @canLendQty, canLendDay = @canLendDay where rdType = @rdType";
                    comm.Parameters.AddWithValue("@rdType", tbRdType.Text);
                    comm.Parameters.AddWithValue("@rdTypeName", tbRdTypeName.Text);
                    comm.Parameters.AddWithValue("@canLendQty", tbCanLendQty.Text);
                    comm.Parameters.AddWithValue("@canLendDay", tbCanLendDay.Text);
                    comm.ExecuteNonQuery();
                    MessageBox.Show("修改成功");
                    DataBind();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败 " + ex.Message);
            }
        }
    }
}
